module app.controller.api.CategoryController;

import std.json;
import hunt.framework;
import app.repository.CategoryRepository;
import app.response.ResultMsg;
import app.utils.RespCodeEnun;
import app.model.Article;
import app.model.Category;
import app.utils.Const;

// import std.stdio;

class CategoryController : Controller
{
    mixin MakeController;

    /**
    * 获取所有分类
    */
    @Action Response list()
    {
        ResultMsg resultMsg = new ResultMsg();
        CategoryRepository categoryRepository = new CategoryRepository();

        try
        {
            auto cates = categoryRepository.findAll();
            JSONValue[] items;
            foreach (cate; cates)
            {
                JSONValue item;
                item["id"] = cate.id;
                item["name"] = cate.name;

                items ~= item;
            }

            resultMsg.code = RespCodeEnun.SUCCESS;
            resultMsg.msg = "success";
            resultMsg.data["data"] = items;
        }
        catch (Exception e)
        {
            this.response.setStatus(500);
            resultMsg.code = RespCodeEnun.ERROR;
            resultMsg.msg = "系统异常";
        }

        return new JsonResponse(resultMsg);

    }

    /**
    * 获取某个分类下所有文章
    * @id   int   分类id
    */
    @Action Response cateArt(long id)
    {
        import std.string;
        import std.random;
        import std.datetime.systime;

        // writeln(id);
        ResultMsg resultMsg = new ResultMsg();
        CategoryRepository categoryRepository = new CategoryRepository();

        try
        {
            Article[] articles = categoryRepository.findArticsleByCateId(id);
            JSONValue[] items;
            foreach (article; articles)
            {
                JSONValue item;
                item["id"] = article.id;
                item["title"] = article.title;
                item["desc"] = article.desc;
                item["view_count"] = article.viewCount;
                item["comment_count"] = article.commentCount;
                item["created_at"] = tr(SysTime(unixTimeToStdTime(article.createdAt))
                        .toISOExtString(), "T", " ");
                item["img"] = Const.imgs.choice();
                items ~= item;
            }

            resultMsg.code = RespCodeEnun.SUCCESS;
            resultMsg.msg = "success";
            resultMsg.data["data"] = items;
        }
        catch (Exception e)
        {
            this.response.setStatus(500);
            resultMsg.code = RespCodeEnun.ERROR;
            resultMsg.msg = "success";
        }
        return new JsonResponse(resultMsg);
    }

    /**
    * 分类详情
    * @id   int   分类id
    */
    @Action Response detail(long id)
    {
        // writeln(id);
        if (id <= 0)
        {
            auto resp = new Response();
            resp.doError(400);
            return resp;
        }
        ResultMsg resultMsg = new ResultMsg();
        CategoryRepository categoryRepository = new CategoryRepository();

        try
        {
            auto cate = categoryRepository.findById(id);
            JSONValue item;
            item["id"] = cate.id;
            item["name"] = cate.name;

            resultMsg.code = RespCodeEnun.SUCCESS;
            resultMsg.msg = "success";
            resultMsg.data["data"] = item;
        }
        catch (Exception e)
        {
            this.response.setStatus(500);
            resultMsg.code = RespCodeEnun.ERROR;
            resultMsg.msg = "success";
        }
        return new JsonResponse(resultMsg);
    }

}
